<template>
  <el-dialog v-model="visible" :title="title" width="420" show-footer show-zoom resize :close-on-click-modal="false"
    :close-on-press-escape="false" @close="close">
    <template #default>
      <el-form ref="formRef" :rules="rules" :model="form" label-align="right" label-width="120" size="default"
        label-position="top">
        <el-form-item label="拣货员编号" prop="number">
          <div class="flex">
            <el-input style="width: 300px;" v-model="form.number" placeholder="请输入拣货员编号"></el-input>
            <el-switch class="ml-2" v-model="form.status" inline-prompt active-text="启用" inactive-text="禁用"
              :active-value='1' :inactive-value='0' />
          </div>
        </el-form-item>
        <el-form-item label="拣货员名称" prop="picker_user_id">
          <select-personnel class="w-full" v-model="form.picker_user_id" placeholder="请选择拣货员"></select-personnel>
        </el-form-item>
        <el-form-item label="所属仓库" prop="warehouse">
          <SelectWarehouse class="w-full" v-model="form.warehouse" placeholder="请输入所属仓库" :multiple="true">
          </SelectWarehouse>
        </el-form-item>
      </el-form>
    </template>
    <template #footer>
      <template v-if="handleType == 'view'">
        <div class="flex justify-center py-2">
          <el-button type="primary" @click="close">关 闭</el-button>
        </div>
      </template>
      <div class="flex justify-center py-2" v-else>
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" :loading="loading" @click="save(formRef)">保 存</el-button>
      </div>

    </template>
  </el-dialog>
</template>
<script setup name="ComModal">
import { getPickerDetail, savePicker, updatePicker } from '../api/index'
import { reactive, ref, toRefs } from 'vue'
import { ElMessage } from 'element-plus'
import SelectWarehouse from '@/components/SelectWarehouse'

const visible = ref(false)
const loading = ref(false)
const formRef = ref(null)
const emits = defineEmits(['query'])
const props = defineProps({
  options: {
    type: Object,
    default: {},
  },
})

const data = reactive({
  form: {},
  title: '新增',
  handleType: null,
  rules: {
    number: [{
      required: true, message: '请输入拣货员编号', trigger: 'blur'
    }],
    picker_user_id: [{
      required: true, message: '请选择拣货员名称', trigger: 'blur'
    }],
    warehouse: [{
      required: true, message: '请选择所属仓库', trigger: 'blur'
    }],
  },
  floorList: [],
  areaList: []
})
const { form, title, handleType, rules, floorList, areaList } = toRefs(data)

const statusMap = new Map([
  ['add', '新增'],
  ['edit', '编辑'],
  ['view', '查看'],
])

const open = (type, id) => {
  title.value = statusMap.get(type)
  handleType.value = type
  visible.value = true
  if (id) getDetail(id)
}

const initForm = () => {
  form.value = getForm()
}
// 初始化
const getForm = () => {
  return {
    status: 1
  }
}

onMounted(() => {
  initForm()
})

const getDetail = async (id) => {
  try {
    const res = await getPickerDetail({
      id
    })
    form.value = res.data
    filterList()
  } catch (e) {
    throw Error(e)
  }
}

const save = () => {
  if (!formRef.value) return
  formRef.value.validate((valid) => {
    if (valid) {
      loading.value = true
      console.log(form.value)
      if (form.value.id) {
        updatePicker(form.value)
          .then((res) => {
            ElMessage.success(res.message)
            emits('query')
            close()
          })
          .finally((e) => {
            loading.value = false
          })
      } else {
        savePicker(form.value)
          .then((res) => {
            ElMessage.success(res.message)
            emits('query')
            close()
          })
          .finally((e) => {
            loading.value = false
          })
      }
    }
  })
}
const close = () => {
  initForm()
  visible.value = false
}
defineExpose({ open })
</script>
<style lang="scss" scoped>
.borderNone {
  :deep(.el-input__wrapper) {
    border: none;
    box-shadow: none
  }
}
</style>
